package com.xinchao.controller;

import com.xinchao.core.constant.ResponseConstant;
import com.xinchao.model.BreakdownImg;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.BreakdownImgService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 终端故障图片
 *
 * @author linqiang
 * @date 2018-08-08 15:37
 */
@Controller
@RequestMapping(value = "/breakdownImg")
public class BreakdownImgController {
    private Logger logger = LoggerFactory.getLogger(BreakdownImgController.class);
    @Autowired
    private BreakdownImgService breakdownImgService;

    /**
     * 获取终端故障图片集合
     *
     * @return Object
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list(String deviceCode) {
        try {
            List<BreakdownImg> breakdownTypeList = breakdownImgService.list(deviceCode);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, breakdownTypeList, "获取终端故障图片集合成功");
        } catch (Exception e) {
            logger.error("BreakdownImgController list 获取终端故障图片集合失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "获取终端故障图片集合失败");
    }

    /**
     * 上传故障图片
     *
     * @param files   故障图片文件
     * @param request 请求
     * @return JsonResponse
     */
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Object upload(Long deviceId, @RequestParam("files") MultipartFile files, HttpServletRequest request) {
        try {
            if (null == deviceId) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "终端ID不能为空");
            }
            //获取文件全名称
            String originalFilename = files.getOriginalFilename();
            if (StringUtils.isBlank(originalFilename)) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "文件名不能为空");
            }
            //去掉后缀，获取纯文件名
            String extension = FilenameUtils.getExtension(originalFilename);
            String fileName = originalFilename.replaceAll("." + extension, "").trim();
            if (StringUtils.isBlank(fileName)) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "文件名不能为空");
            }
            List<Map<String, Object>> msgList = new ArrayList<>();
            Map<String, Object> fileMsgMap = breakdownImgService.save(deviceId, files, request);
            msgList.add(fileMsgMap);
            return msgList;
        } catch (Exception e) {
            logger.error("BreakdownImgController upload 上传故障图片失败：" + e.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, "上传故障图片失败");
        }
    }

    /**
     * 删除故障图片
     *
     * @param breakdownImgId 故障图片ID
     * @param request        请求
     * @return object
     */
    @ResponseBody
    @RequestMapping(value = "/delete/{breakdownImgId}")
    public Object delete(@PathVariable Long breakdownImgId, HttpServletRequest request) {
        if (null == breakdownImgId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "故障图片ID不能为空");
        }
        try {
            int row = breakdownImgService.deleteByPrimaryKey(breakdownImgId, request);
            if (row > 0) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, "删除故障图片成功");
            }
        } catch (Exception e) {
            logger.error("BreakdownImgController delete 删除故障图片失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "删除故障图片失败");
    }
}
